package me.boot.web.common.template.impl;

import io.swagger.annotations.ApiOperation;
import me.boot.db.mybatis.template.entity.Entity;
import me.boot.web.common.template.SuperCacheService;
import me.boot.web.common.template.model.Result;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;

/**
 * SuperCacheController
 * <p>
 * 继承该类，在SuperController类的基础上扩展了以下方法：
 * 1，get ： 根据ID查询缓存，若缓存不存在，则查询DB
 */
public abstract class SuperCacheControllerImpl<
        ID extends Serializable,
        PO extends Entity<ID>,
        S extends SuperCacheService<ID, PO>,
        DTO>
        extends SuperControllerImpl<ID, PO, S, DTO> {

    @Override
    public Result<PO> getById(ID id) {
        return Result.newSuccess(service.getFromCache(id));
    }

    /**
     * 刷新缓存
     *
     * @return 是否成功
     */
    @ApiOperation(value = "刷新缓存", notes = "刷新缓存")
    @PostMapping("/cache/refresh")
    public Result<Boolean> refreshCache() {
        service.refreshCache();
        return Result.newSuccess(true);
    }

    /**
     * 清理缓存
     *
     * @return 是否成功
     */
    @ApiOperation(value = "清理缓存", notes = "清理缓存")
    @PostMapping("/cache/clear")
    public Result<Boolean> clearCache() {
        service.clearCache();
        return Result.newSuccess(true);
    }
}
